// loading script file

void read_macro(std::istream& ifl, string mname) {
  char sline[2048];
  bool fine=false;
  string smac;
  
  while(ifl.good()) {
    ifl.getline(sline,2047);
    
    for(int i=1;i<2048;i++) {
      if(sline[i]==0x0) break;
      if(sline[i]=='#') {sline[i]=0x0;break;}
    }
    
    if(sline[0]==0x0) continue;
          
    if(string(sline)=="@end") {fine=true;break;}
    smac.append(sline);
    smac.append(";");
  }
  
  if(fine) {
    macname.push_back(mname);
    macstring.push_back(smac);
  } else throw (string("macro @end mark required for ")+mname).c_str();
  
}

void cmd_load(istringstream& ss) {
  if(looplevel) throw "not allowed: load inside loop";
  if(loading) throw "not allowed: recursive load";
  
  loading=true;

  string scname;
  if(ss>>scname) {
    char sline[2048];
    int nline;
    vector<string> sinput;
    if(me==0) {
      ifstream ifl(scname.c_str());
      while(ifl.good()) {
        ifl.getline(sline,2047);
        if(string(sline).substr(0,6)=="@macro") {
          istringstream ms(sline);
          string mname;
          ms>>mname;
          ms>>mname;
          read_macro(ifl,mname);
        }
        else sinput.push_back(sline);
      }
      nline=(int)sinput.size();
    }
    
#ifndef DRIFT_NO_MPI
    
    MPI_Bcast(&nline,1,MPI_INT,0,MPI_COMM_WORLD);
    for(int i=0;i<(int)nline;i++) {
      memset(sline,0,2048);
      if(me==0) sinput[i].copy(sline,2048);
      MPI_Bcast(sline, 2048, MPI_CHAR, 0, MPI_COMM_WORLD);
      parse(sline);
    }
    
#else
    for(int i=0;i<(int)sinput.size();i++) parse(sinput[i]);
#endif

  } else throw "script name required";
               
  loading=false;
} 
